#!/bin/zsh

# run app for jar file
if ! java -version >/dev/null 2>&1; then
  echo "java env error"
  exit 1
fi

SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)


TARGET_DIR="target/"
if [ ! -d "${SCRIPT_DIR}/$TARGET_DIR" ]; then
  # mvn package
  FAILURE_TIP="BUILD FAILURE"
  result=$( (mvn clean && mvn package) | grep "${FAILURE_TIP}")
  if [[ ${result} =~ ${FAILURE_TIP} ]]; then
    echo "mvn build failure"
    exit 3
  fi
fi

JAR_PATH="${SCRIPT_DIR}/$TARGET_DIR/student-project-1.0-SNAPSHOT-jar-with-dependencies.jar"
if [ ! -f "$JAR_PATH" ]; then
  echo "jar file is not exist"
  exit 4
fi

# run app
java -jar "$JAR_PATH"

